﻿using System;
using System.Collections.Generic;
using System.IO;

namespace UnityModule
{
    public partial class VersionComponent
    {
        Dictionary<string, AssetBundleInfoV2> oldBundleInfos;

        private byte[] ReadLocalBundleInfoFile(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Open);
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);
            fs.Close();
            return data;
        }

        public void ClearOldBundle(Dictionary<string, string> bundleInfosWithHash)
        {           
            string path = ApplicationPath.ABCachePath + ApplicationPath.BundleInfoFile;
            if (File.Exists(path))
            {
                System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                sw.Start();
                oldBundleInfos = DeserializeBundleBin(ReadLocalBundleInfoFile(path)); 
                CompareOldFile(bundleInfosWithHash);                
                sw.Stop();
                //P.E("ClearOldBundle cost:" + sw.Elapsed.ToString() + " Delete File:" + deleteCount);
            }
         
        }
        int deleteCount = 0;
        private void CompareOldFile(Dictionary<string, string> bundleInfosWithHash)
        {
            if (oldBundleInfos != null)
            {
                deleteCount = 0;
                try
                {
                    //保存 bundleNameWithHash->bundleName 对应关系
                    foreach (var item in oldBundleInfos)
                    {
                        if (!bundleInfosWithHash.ContainsKey(item.Value.abNameWithHash))
                        {
                            deleteCount++;
                            if (File.Exists(ApplicationPath.ABCachePath + item.Value.abNameWithHash))
                                File.Delete(ApplicationPath.ABCachePath + item.Value.abNameWithHash);
                            //P.E("delete old " + ApplicationPath.ABCachePath + item.Value.abNameWithHash);
                        }
                    }
                }
                catch (Exception e) {
                    P.E("clear old bundle:"+e.ToString());
                }
            }
        }


        public void ForeachAbFiles(string path)
        {
            try
            {
                List<string> files = tility.PathTool.GetFiles(path);
                int count = 0;

                for (int i = 0; i < files.Count; i++)
                {
                    if (!files[i].EndsWith("bin") && !files[i].EndsWith("update"))
                    {
                        string hashPath = tility.PathTool.GetRegularPath(files[i].Replace(path, "")).TrimStart('/');
                        if (!CdnBundleInfosWithHash.ContainsKey(hashPath))
                        {
                            count++;
                            File.Delete(files[i]);
                            //P.D($"delete old ({count})" + files[i]);
                        }
                    }
                }
            }
            catch (Exception e) {
                P.E(e);
            }
        }
    }
}
